package nc.pub.utils;

import com.cmg.CommonUtils;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
  
public class HttpRequestUtil {  
  
  
    public static JSONObject getToken() throws Exception {  
    	String URL = CommonUtils.getRestInfo("post")+CommonUtils.getRestInfo("gettoken");
        Map<String, String> headers =  new HashMap();
        
		headers.put("Content-Type", "application/json");
        String loginId = CommonUtils.getRestInfo("user");
        String loginKey = CommonUtils.getRestInfo("password");
        headers.put("loginId", loginId);  
        headers.put("loginKey", loginKey); 
        
        JSONObject json = sendGet(URL, headers);
        return json;  
    }
    
    public static JSONObject sendGet(String urlString, Map<String, String> headers) throws IOException, JSONException {  
        URL url = new URL(urlString);  
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();  
        connection.setRequestMethod("GET");  
  
        if (headers != null) {  
            for (Map.Entry<String, String> entry : headers.entrySet()) {  
                connection.setRequestProperty(entry.getKey(), entry.getValue());  
            }  
        }  
  
        int responseCode = connection.getResponseCode();  
        if (responseCode == HttpURLConnection.HTTP_OK) { // success  
            try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {  
                String inputLine;  
                StringBuilder response = new StringBuilder();  
  
                while ((inputLine = in.readLine()) != null) {  
                    response.append(inputLine);  
                }  
                return new JSONObject(response.toString());  
            }  
        } else {  
            throw new RuntimeException("GET request failed: HTTP error code : " + responseCode);  
        }  
    }  
  
    public static JSONObject sendPost(String urlString, String payload, Map<String, String> headers) throws IOException, JSONException {  
        URL url = new URL(urlString);  
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();  
        connection.setRequestMethod("POST");  
        connection.setRequestProperty("Content-Type", "application/json; utf-8");  
        connection.setRequestProperty("Accept", "application/json");  
        connection.setDoOutput(true);  
  
        if (headers != null) {  
            for (Map.Entry<String, String> entry : headers.entrySet()) {  
                connection.setRequestProperty(entry.getKey(), entry.getValue());  
            }  
        }  
  
        try (OutputStream os = connection.getOutputStream()) {  
            byte[] input = payload.getBytes(StandardCharsets.UTF_8);  
            os.write(input, 0, input.length);  
        }  
  
        int responseCode = connection.getResponseCode();  
        if (responseCode == HttpURLConnection.HTTP_CREATED || responseCode == HttpURLConnection.HTTP_OK) { // success  
            try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {  
                String inputLine;  
                StringBuilder response = new StringBuilder();  
  
                while ((inputLine = in.readLine()) != null) {  
                    response.append(inputLine);  
                }  
                return new JSONObject(response.toString());  
            }  
        } else {  
            throw new RuntimeException("POST request failed: HTTP error code : " + responseCode);  
        }  
    }
}